//
//  LWMeCtrl.m
//  laowu
//
//  Created by 尹桥印 on 15/4/24.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "LWMeCtrl.h"
#import "LWLoginCtrl.h"
#import "MeItemCell.h"
#import "MeItemInfo.h"
#import "MeHeaderView.h"
#import "LWWorkReportListCtrl.h"
#import "LWFeedBackCtrl.h"
#import "LWModifyPwdCtrl.h"
#import "LWMigrantProfileCtrl.h"
#import "LWMineMsgCtrl.h"
#import "LWPeixunListCtrl.h"
#import "LWCompanyProfileCtrl.h"
#import "LWJobListCtrl.h"
#import "LWLeaderProfileCtrl.h"
#import "LWTechProfileCtrl.h"
#import "LWProxyProfileCtrl.h"
#import "LWLifeCommonCtrl.h"

@interface LWMeCtrl ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong)NSMutableArray *itemArray;
@property (nonatomic ,weak)MeHeaderView *headerView;

@property (nonatomic ,strong) MeItemInfo *report;
@property (nonatomic ,strong) MeItemInfo *job;
@property (nonatomic ,strong) MeItemInfo *peixun;

@end


@implementation LWMeCtrl

-(NSMutableArray *)itemArray{
    if (!_itemArray) {
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"个人中心"];
    
//    [self.itemArray addObject:[MeItemInfo itemWithIcon:@"lw_info" title:@"个人资料"]];
//    [self.itemArray addObject:[MeItemInfo itemWithIcon:@"lw_work" title:@"工作汇报"]];
//    [self.itemArray addObject:[MeItemInfo itemWithIcon:@"lw_job" title:@"职位申请"]];
//    [self.itemArray addObject:[MeItemInfo itemWithIcon:@"lw_peixun-1" title:@"培训申请"]];
//    [self.itemArray addObject:[MeItemInfo itemWithIcon:@"lw_psw" title:@"密码修改"]];
//    [self.itemArray addObject:[MeItemInfo itemWithIcon:@"lw_notes" title:@"我的消息"]];
//    [self.itemArray addObject:[MeItemInfo itemWithIcon:@"lw_suggestions" title:@"意见反馈"]];
//    [self.itemArray addObject:[MeItemInfo itemWithIcon:@"lw_out" title:@"退出"]];
    
    [self.itemArray addObject:[MeItemInfo itemWithIcon:@"lw_info" title:@"个人资料" operation:^{
        NSInteger group_id = [UserDefaultsHelper integerForKey:KEY_USER_GROUP_ID];
        if(group_id == 3||group_id == 10||group_id==11){//劳务公司身份资料
         LWCompanyProfileCtrl *companyCtrl = [[LWCompanyProfileCtrl alloc]init];
         [self.navigationController pushViewController:companyCtrl animated:YES];
        }else if(group_id == 1){//民工身份资料
         LWMigrantProfileCtrl *migrantCtrl = [[LWMigrantProfileCtrl alloc]init];
         [self.navigationController pushViewController:migrantCtrl animated:YES];
        }else if (group_id == 2){//班组长
         LWLeaderProfileCtrl *leaderCtrl = [[LWLeaderProfileCtrl alloc]init];
         [self.navigationController pushViewController:leaderCtrl animated:YES];
        }else if(group_id ==  4||group_id ==  5||group_id ==  6||group_id ==  7||group_id ==  8){//技术员工
         LWTechProfileCtrl *techCtrl = [[LWTechProfileCtrl alloc]init];
         [self.navigationController pushViewController:techCtrl animated:YES];
        } else if(group_id == 12||group_id == 13){//代理机构
            LWProxyProfileCtrl *proxyCtrl = [[LWProxyProfileCtrl alloc]init];
            [self.navigationController pushViewController:proxyCtrl animated:YES];
        }
        
    }]];
    
    self.report = [MeItemInfo itemWithIcon:@"lw_work" title:@"工作汇报" operation:^{
        LWWorkReportListCtrl *ctrl = [[LWWorkReportListCtrl alloc] init];
        [self.navigationController pushViewController:ctrl animated:YES];
    }];
    
    self.job =[MeItemInfo itemWithIcon:@"lw_job" title:@"职位申请" operation:^{
        LWJobListCtrl *ctrl = [[LWJobListCtrl alloc] init];
        [self.navigationController pushViewController:ctrl animated:YES];
    }];
    
    self.peixun = [MeItemInfo itemWithIcon:@"lw_peixun-1" title:@"培训申请" operation:^{
        LWPeixunListCtrl *ctrl = [[LWPeixunListCtrl alloc] init];
        [self.navigationController pushViewController:ctrl animated:YES];
        
    }];
    
    [self.itemArray addObject:[MeItemInfo itemWithIcon:@"baike" title:@"生活百科"  operation:^{
        LWLifeCommonCtrl *ctrl = [[LWLifeCommonCtrl alloc] init];
        [self.navigationController pushViewController:ctrl animated:YES];
    }]];
    
    [self.itemArray addObject:[MeItemInfo itemWithIcon:@"lw_psw" title:@"密码修改"  operation:^{
        LWModifyPwdCtrl *ctrl = [[LWModifyPwdCtrl alloc] init];
        [self.navigationController pushViewController:ctrl animated:YES];
    }]];
    [self.itemArray addObject:[MeItemInfo itemWithIcon:@"lw_notes" title:@"我的消息" operation:^{
         LWMineMsgCtrl *ctrl = [[LWMineMsgCtrl alloc] init];
        [self.navigationController pushViewController:ctrl animated:YES];
    }]];
    [self.itemArray addObject:[MeItemInfo itemWithIcon:@"lw_suggestions" title:@"意见反馈"  operation:^{
        LWFeedBackCtrl *ctrl = [[LWFeedBackCtrl alloc] init];
        [self.navigationController pushViewController:ctrl animated:YES];
    }]];
    [self.itemArray addObject:[MeItemInfo itemWithIcon:@"lw_out" title:@"退出" operation:^{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"确定退出吗？" message:@"" delegate: nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
         alert.delegate = self;
        [alert show]; //显示退出对话框

    }]];
    
    MeHeaderView *headerView = [[MeHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREENW, SCREENW/2.28) nibNameOrNil:nil];
    self.headerView = headerView;
    [self.tableView setTableHeaderView:headerView];
    [self.tableView setTableFooterView:[UIView new]];
    [self.tableView setDataSource:self];
    [self.tableView setDelegate:self];
}

#warning 待写的功能（删除本地保存信息的操作跳转页面）
/**
 *  退出登录之后需要跳转的页面
 *
 *  @param alertView   <#alertView description#>
 *  @param buttonIndex buttonIndex description
 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1) {
        [self cleanInfo];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"toHomeCtrl" object:nil];
    }
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MeItemCell *cell = [MeItemCell cellWithTableView:tableView indexPath:indexPath];
    [cell setItemInfo:[self.itemArray objectAtIndex:indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MeItemInfo *itemInfo = [self.itemArray objectAtIndex:indexPath.row];
    if (itemInfo.opration) {
        itemInfo.opration();
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (!ISLOGIN) {
        LWLoginCtrl *ctrl = [[LWLoginCtrl alloc] init];
        [self presentViewController:[[CustomNavigationCtrl alloc] initWithRootViewController:ctrl] animated:YES completion:nil];
    }else{
        if (!ISBLANK([UserDefaultsHelper stringForKey:KEY_USER_IMG])) {
            NSLog(@"%@",[UserDefaultsHelper stringForKey:KEY_USER_IMG]);
            [self.headerView.headerImg sd_setImageWithURL:[NSURL URLWithString:[UserDefaultsHelper stringForKey:KEY_USER_IMG]]];
        }else{
            [self.headerView.headerImg setImage:[UIImage imageNamed:@"me_headimg_default"]];
        }
        [self.headerView.nameLabel  setText:[UserDefaultsHelper stringForKey:KEY_USER_REAL_NAME]];
    }
    
    NSInteger group_id = [UserDefaultsHelper integerForKey:KEY_USER_GROUP_ID];
    
    [self.itemArray removeObject:self.report];
    [self.itemArray removeObject:self.job];
    [self.itemArray removeObject:self.peixun];
    
    //3 10 11 12 13  代表不是企业用户
    if (!(group_id== 3 ||group_id== 10 ||group_id== 11 ||group_id== 12 ||group_id== 13)) {
        
        [self.itemArray insertObject:self.report atIndex:1];
        [self.itemArray insertObject:self.job atIndex:2];
        [self.itemArray insertObject:self.peixun atIndex:3];
    }
    [self.tableView reloadData];
}

-(void)cleanInfo{
    [UserDefaultsHelper setObject:nil forKey:KEY_USER_REAL_NAME];
    [UserDefaultsHelper setObject:nil forKey:KEY_USER_ID];
    [UserDefaultsHelper setObject:nil forKey:KEY_USER_GROUP_NAME];
    [UserDefaultsHelper setObject:nil forKey:KEY_USER_IMG];
    [UserDefaultsHelper setObject:nil forKey:KEY_USER_GROUP_ID];
}


@end
